package cn.sm1234.controller;

import cn.sm1234.domain.Customer;
import cn.sm1234.service.CustomerService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/customer")
public class CustomerController {

    // 用resource注解自动生成对象， 指定为应用程序中的一个资源组件类。当前类初始化时， 会注入该类
    @Resource
    private CustomerService customerService;

    // 跳转到input页面
    @RequestMapping("/input")
    public String input(){
        return  "input";
    }

    @RequestMapping("/save")
    public String save(Customer customer){
        customerService.save(customer);
        return "success";
    }

    @RequestMapping("/list")
    public String list(Model model){
        List<Customer> list = customerService.findAll();
        model.addAttribute("list", list);
        return  "list";
    }

    /*根据ID查询*/
    @RequestMapping("/findById")
    public String findById(Integer id, Model model){
        Customer customer = customerService.findById(id);
        model.addAttribute("cust", customer);
        return "input";
    }

    /*删除客户*/
    @RequestMapping("/delete")
    public  String delete(Integer id){
        customerService.delete(id);
        return "redirect:/customer/list";
    }
}
